const api = require('../../api/api.js');

Page({
  data: {},
  formSubmit(e) {
    let account = e.detail.value.account.trim();
    let psd = e.detail.value.psd.trim();
    if (account === '' || psd === '') {
      wx.showToast({
        title: '请输入账号或密码！',
        icon: 'none'
      });
      return;
    }
    this.login({
      account: account,
      psd: psd
    });
  },
  login(params) {
    let that = this;
    api.login({
      data: {
        account: params.account,
        password: params.psd
      },
      success(res) {
        console.log(res.data);
        let data = res.data;
        if (data.type === 1) {
          wx.showToast({
            title: '账号不存在！',
            icon: 'none'
          });
        } else if (data.type === 2) {
          wx.showToast({
            title: '密码错误！',
            icon: 'none'
          });
        } else {
          that.setData({
            login: true
          });
          wx.setStorage({
            key: 'userId',
            data: params.account,
          });
          //在这里添加跳转页面的代码
        }
      }
    });
  },
})